#region File Description
//-----------------------------------------------------------------------------
// PhoneMainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

using System;
using Outliers;
using Microsoft.Xna.Framework;
using System.IO.IsolatedStorage;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
namespace OutliersSample
{
    class PhoneMainMenuScreen : PhoneMenuScreen 
    {
        public PhoneMainMenuScreen()
            : base("") // used to say "Main Menu"

           // bool musicman = true;
        {
            // Create a button to start the game
            Button playButton = new Button("Play");
            playButton.Tapped += playButton_Tapped;
            MenuButtons.Add(playButton);

            var settings = IsolatedStorageSettings.ApplicationSettings;
            bool soundFxCanPlay;
            bool musicCanPlay;

            //
            // MANAGES THE SOUND EFX SETTING ON A APP RESTART
            //
            //if the setting has already been created
            if (settings.Contains("SoundEffectsSetting"))
            {

                //Retrieve data stored in the settings
                string determineFxSettingAfterRelaunch = settings["SoundEffectsSetting"].ToString();

                if (determineFxSettingAfterRelaunch == "false")
                {
                    soundFxCanPlay = false;
                    //MediaPlayer.Stop();
                }
                else
                {
                    soundFxCanPlay = true;
                }
            }
            else //if the isolated storage setting hasn't been created yet, then create it and set it to "off" to turn off music.
            {
                //should only hit this the first time the app is run
                soundFxCanPlay = true;
            }


            //
            // MANAGES THE MUSIC SETTING ON A APP RESTART
            //
            //if the setting has already been created
            if (settings.Contains("MusicSetting"))
            {

                //Retrieve data stored in the settings
                string determineMusicSettingAfterRelaunch = settings["MusicSetting"].ToString();

                if (determineMusicSettingAfterRelaunch == "false")
                {
                    musicCanPlay = false;
                    //MediaPlayer.Stop(); //11-14-2012
                }
                else
                {
                    musicCanPlay = true;
                }
            }
            else //if the isolated storage setting hasn't been created yet, then create it and set it to "off" to turn off music.
            {
                //should only hit this the first time the app is run
                musicCanPlay = true;
            }


            // Create two buttons to toggle sound effects and music. This sample just shows one way
            // of making and using these buttons; it doesn't actually have sound effects or music
            BooleanButton sfxButton = new BooleanButton("Sound Effects", soundFxCanPlay);
            sfxButton.Tapped += sfxButton_Tapped;
            MenuButtons.Add(sfxButton);


            BooleanButton musicButton = new BooleanButton("Music", musicCanPlay);
            musicButton.Tapped += musicButton_Tapped;
            MenuButtons.Add(musicButton);

           
        }

        void playButton_Tapped(object sender, EventArgs e)
        {
            // When the "Play" button is tapped, we load the GameplayScreen
            LoadingScreen.Load(ScreenManager, true, PlayerIndex.One, new GameplayScreen());
        }

        void sfxButton_Tapped(object sender, EventArgs e)
        {
            BooleanButton button = sender as BooleanButton;

            // In a real game, you'd want to store away the value of 
            // the button to turn off sounds here. :)
            var settings = IsolatedStorageSettings.ApplicationSettings;

            //if the setting has already been created
            if (settings.Contains("SoundEffectsSetting"))
            {

                //Retrieve data stored in the settings
                string determineFxSettingInApp = settings["SoundEffectsSetting"].ToString();

                if (determineFxSettingInApp == "false")
                {
                    settings["SoundEffectsSetting"] = "true";
                }
                else
                {
                    settings["SoundEffectsSetting"] = "false";
                  //  MediaPlayer.Stop();
                }

            }
            else //if the isolated storage setting hasn't been created yet, then create it and set it to "off" to turn off music.
            {
                settings.Add("SoundEffectsSetting", "false");
                //MediaPlayer.Stop();
            }

            //Retrieve data stored in the settings
            //string location = settings["SoundEffectsSetting"].ToString();

            //modifying the setting if it exists.
            //settings["SoundEffectsSetting"] = "on";


        }

        //music button on/off
        void musicButton_Tapped(object sender, EventArgs e)
        {
            BooleanButton button = sender as BooleanButton;

      
            // In a real game, you'd want to store away the value of 
            // the button to turn off music here. :)
            var settings = IsolatedStorageSettings.ApplicationSettings;

            //if the setting has already been created
            if (settings.Contains("MusicSetting"))
            {

                //Retrieve data stored in the settings
                string determineMusicSettingInApp = settings["MusicSetting"].ToString();

                if (determineMusicSettingInApp == "false")
                {
                    settings["MusicSetting"] = "true";
                //   MediaPlayer.IsRepeating = true; //Groton wakes from coma
                  //  MediaPlayer.Play(content.Load<Song>("Sounds/Soundtrack/OutliersMainMenuMusic"));
                }
                else
                {
                    settings["MusicSetting"] = "false";
                    //MediaPlayer.Stop(); //11-14-2012
                }

            }
            else //if the isolated storage setting hasn't been created yet, then create it and set it to "off" to turn off music.
            {
                settings.Add("MusicSetting", "false");
                MediaPlayer.Stop();
            }
    
      
            
        }

        protected override void OnCancel()
        {
            ScreenManager.Game.Exit();
            base.OnCancel();
        }
    }
}
